package crew.blt.handsound.shared.domain;

import java.io.Serializable;

import crew.gwt.client.domain.PersistableEntity;

public class Auth implements PersistableEntity<Auth>,Serializable
{
	private Integer id;
	private String authority;
	private User user;
	
	public Auth()
	{}
	
	public Auth(User user, String authority) 
	{
		this(user, AuthType.valueOf(authority));
	}
	
	public Auth(User user, AuthType authority) 
	{
		this.setUser(user);
		this.setAuthorityType(authority);
	}
	

	@Override
	public Auth getCopy(){
		return new Auth(user,authority);
	}
	
	public Integer getId() {
		return id;
	}

	private void setId(Integer id) {
        this.id = id;
    }

	public void setAuthorityType(AuthType authority) {
		this.authority = authority.name();
	}
	
	private void setAuthority(String authority) {
		this.authority = authority;
	}

	public String getAuthority() {
		return authority;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}

	@Override
	public String toString() {
		return getAuthority();
	}
	
	public boolean equals(Object other) {
        if (this == other) return true;
        if ( !(other instanceof Auth) ) return false;

        final Auth auth = (Auth) other;

        if ( !auth.getAuthority().equals( getAuthority() ) ) return false;
        if ( !auth.getUser().equals( getUser() ) ) return false;

        return true;
    }

    public int hashCode() {
//        int result = 0;
//        int result = getUser().hashCode();
//        result = (29 * result) + getAuthority().hashCode();
        return getAuthority().hashCode();
    }

	@Override
	public String[] getHeadersValues() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String[] getValues() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public String getIdFieldName() {
		return "id";
	}
}
